package com.gnerv.plug.note.ui;

import com.gnerv.plug.note.data.DataCenter;
import com.gnerv.plug.note.dialog.CreateNoteDialog;
import com.gnerv.plug.note.entity.NoteInfo;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import lombok.Data;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Data
public class NoteToolWindow {

    private JPanel rootPanel;
    private JTextField noteTitle;
    private JTable noteTable;
    private JButton button1;

    private Project project;
    private ToolWindow toolWindow;

    private DefaultTableModel model;

    public NoteToolWindow() {
    }

    public NoteToolWindow(Project project, ToolWindow toolWindow) {
        this.project = project;
        this.toolWindow = toolWindow;
        init();
        hideColumn(0);
        addMouseListener();
    }

    public void init() {
        model = new DefaultTableModel(null, DataCenter.HEADERS) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        noteTable.setModel(model);
        noteTable.setEnabled(true);
    }

    public void addData(String[] rowData) {
        model.addRow(rowData);
    }

    public void addData(NoteInfo noteInfo) {
        String[] cellData = new String[3];
        cellData[0] = noteInfo.getUkId();
        cellData[1] = noteInfo.getTitle();
        cellData[2] = noteInfo.getContent();
        addData(cellData);
    }

    public void hideColumn(int index) {
        this.noteTable.getColumnModel().getColumn(index).setWidth(0);
        this.noteTable.getColumnModel().getColumn(index).setMaxWidth(0);
        this.noteTable.getColumnModel().getColumn(index).setMinWidth(0);
        this.noteTable.getTableHeader().getColumnModel().getColumn(index).setWidth(0);
        this.noteTable.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(0);
        this.noteTable.getTableHeader().getColumnModel().getColumn(index).setMinWidth(0);
    }

    private void addMouseListener() {
        this.noteTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int selectedRow = noteTable.getSelectedRow();
                    NoteInfo noteInfo = getRowDate(selectedRow);
                    CreateNoteDialog createNoteDialog = new CreateNoteDialog(noteInfo);
                    createNoteDialog.show();
                }
            }
        });
        this.noteTitle.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                if((char)e.getKeyChar()==KeyEvent.VK_ENTER) {
                    DataCenter.refreshToolWindowTableData(noteTitle.getText());
                }
            }
        });
    }

    public NoteInfo getRowDate(int rowIndex) {
        String ukId = (String) this.noteTable.getValueAt(rowIndex, 0);
        String title = (String) this.noteTable.getValueAt(rowIndex, 1);
        String noteTable = (String) this.noteTable.getValueAt(rowIndex, 2);
        NoteInfo noteInfo = NoteInfo.builder().ukId(ukId).title(title).content(noteTable).build();
        return noteInfo;
    }
}
